#include "CommandManager.h"

#define MSG_ERR_UNK_COM cout << "unknown command" << endl;
#define MSG_ERR_INC_COM cout << "incomplete command" << endl;

Database CommandManager::_dbMain;

void CommandManager::test() {
    //remove("databases/basex64.db");
	//DiscManager::createNewDatabase("basex64");
	//SubjectManager::newSubject("Stefan Clare");
	//PredicateManager::newPredicate("studiert");
	//SubjectManager::newSubject("Informatik");
	//sTripelId tripelId1 = TripelManager::newTripel(sTripel{ "Stefan Clare", "studiert", "Informatik" });
	//SubjectManager::newSubject("Marius Erbert");
	//PredicateManager::newPredicate("studiert");
	//SubjectManager::newSubject("Informatik");
	//sTripelId tripelId2 = TripelManager::newTripel(sTripel{ "Marius Erbert", "studiert", "Informatik" });
	DiscManager::useDatabase("basex64");
	sTripelId tripelId1 = sTripelId{ 1, 1 };
	sTripelId tripelId2 = sTripelId{ 1, 1 };
    sTripel tripel = TripelManager::getTripel(tripelId1);
    cout << "( '" + tripel.subject + "', '" + tripel.predicate + "', '" + tripel.object + "' )" << endl;
    tripel = TripelManager::getTripel(tripelId2);
    cout << "( '" + tripel.subject + "', '" + tripel.predicate + "', '" + tripel.object + "' )" << endl;
    BufferManager::clear();
}

void CommandManager::reveiveCommands() {
    string input = "";
    cout << "Triple-DMS" << endl;
    while (input != "exit") {
        cout << ">";
        getline(cin, input);
        if (input == "test") {
            test();
        }
        else {
            QueryResult res = _dbMain.executeQuery(input);
            while (!res.isEmpty()) {
                sTripel tripel = res.getNextResult();
                cout << "( '" + tripel.subject + "', '" + tripel.predicate + "', '" + tripel.object + "' )" << endl;
            }

            cout << res.getMessage() << endl;
        }
    }

    BufferManager::clear();

}
